package wechat.rcv.controller

import com.shrcl.engineerprocess.domain.CertificationNotification
import com.shrcl.engineerprocess.domain.EngineerProjectInitNotification
import com.shrcl.engineerprocess.domain.Notification
import com.shrcl.engineerprocess.domain.TrainingNotification
import edu.fudan.mylang.pf.IObjectFactory
import org.apache.log4j.Logger
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.stereotype.Controller
import org.springframework.ui.Model
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RequestMethod
import java.text.SimpleDateFormat
import org.springframework.web.bind.annotation.RequestParam

@Controller
class NotificationDispatcher {
	final static Logger logger = Logger.getLogger(typeof(NotificationDispatcher).getName());
 	@Autowired IObjectFactory entities;
//	@RequestMapping(value="/notificationdetail", method = RequestMethod.GET)
	@RequestMapping(value="/notificationdetail")
	def String process(
		@RequestParam(value="notificationId", defaultValue="") Long notificationId,
		Model model
	) {
		logger.info('=========>NotificationDispatcher.processs<============')
		logger.info('=========>NotificationDispatcher.notificationId<============'+notificationId)
		
		var hpage = 'error'
		
		try{
			val n = entities.get(typeof(Notification), notificationId);
			logger.info('=========>NotificationDispatcher.type<============'+n.getNotificationType)
			if(n == null){
				model.addAttribute("errormsg", "Invalid id");
			}
			else if (n instanceof EngineerProjectInitNotification){
				println('=======>NotificationDispatcher.EngineerProjectInitNotification<========')
				model.addAttribute("notificationId", notificationId);
				model.addAttribute("noti_weekday", new WXNotification().getWeekDay(n.getNotifyDate.getDay));
				model.addAttribute("noti_date", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(n.getNotifyDate));
				model.addAttribute("name", n.getEngineerCostEstimation.getProject().getName());
				model.addAttribute("customer", n.getEngineerCostEstimation.getProject().getCustomer().getName());
				model.addAttribute("salemanager", n.getEngineerCostEstimation.getPostSaleManager().getName());
				hpage = 'tscnotification'
			}else if (n instanceof TrainingNotification){
				println('=======>NotificationDispatcher.TrainingNotification<========')
				model.addAttribute("notificationId", notificationId);
				model.addAttribute("noti_weekday", new WXNotification().getWeekDay(n.getNotifyDate.getDay));
				model.addAttribute("noti_date", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(n.getNotifyDate));
				model.addAttribute("name", n.getEngineerTraining.getName);
				model.addAttribute("topic", n.getEngineerTraining.getTopic);
				model.addAttribute("trainer", n.getEngineerTraining.getTrainer);
				model.addAttribute("location", n.getEngineerTraining.getLocation);
				model.addAttribute("startDate", new SimpleDateFormat("yyyy-MM-dd").format(n.getEngineerTraining.getStartdate));
				model.addAttribute("endDate", new SimpleDateFormat("yyyy-MM-dd").format(n.getEngineerTraining.getEnddate));
				hpage = 'trainingnotification'
			}else if (n instanceof CertificationNotification){
				println('=======>NotificationDispatcher.<========CertificationNotification')
				
			}
		} catch(Exception e){
			e.printStackTrace
		}
		hpage;
	}
}